<?php
//include cac thu vien khac
include_once 'ExportToExcel.php';
include_once 'ImportFromExcel.php';

/**
 * 
 * Class extend from Zend_Controller_Action
 * @author Vo Xuan Hoan
 *
 */
class ExtLb_Controller_Action extends Zend_Controller_Action{
	
	private $exportExcel ; //obj cua lop xuat du lieu ra filr excel;
	private $importFromExcel; //obj cua library import du lieu tu file excel
	
	/**
	 * 
	 * Export du lieu ra file excel
	 */
	function exportToExcel($arrayData, $arrTitle){
		
		$this->exportExcel = new QuizLb_Controller_ExportToExcel('UTF-8', false);

//		echo "<pre>";
//		print_r($this->exportExcel->addRowTitle($arrTitle, $arrayData));
		$this->exportExcel->addArray(
					$this->exportExcel->addRowTitle($arrTitle, $arrayData)
				);
		
		$this->exportExcel->generateXML();
			
	}
	
	/**
	 * 
	 * import du lieu tu file excel
	 */
	function importFromExcel($fileName){
		$this->importFromExcel = new Spreadsheet_Excel_Reader($fileName,true,'UTF-8');
		return $this->importFromExcel;
	}
	
	/**
   	* 
	* load layout tuong ung voi module 
	* @param unknown_type $moduleName: Ten cua module 
	* @param unknown_type $layoutName: ten cua layout se duoc hien thi cho module 
	*/
	public function loadLayout($moduleName,$layoutName){
    //load layout admin trong templetes      
	   	$layoutPath = LAYOUT_PATH  . '/'.$moduleName;
	    $option = array ('layout' => $layoutName, 
	                  'layoutPath' => $layoutPath );
	    Zend_Layout::startMvc ( $option );		   
	} 
	
	/**
	 * 
	 * Load file css cho action hien tai
	 * @param unknown_type $module: ten module
	 * @param unknown_type $cssName: ten file css
	 */
	public function loadCss($module,$cssName,$nameFolderRoot=NULL){
		if ($nameFolderRoot ==NULL){
			$this->view->headLink()->appendStylesheet(
			'/public/templates/'.$module.'/css/'.$cssName.'.css');
		}else{
			$this->view->headLink()->appendStylesheet(
				'/'.$nameFolderRoot.'/public/templates/'.$module.'/css/'.$cssName.'.css');
		}
	}
	
	/**
	 * 
	 * Load theo path cua file css
	 * Exp: /public/css/style.css
	 * @param unknown_type $path: duong dan toi file css
	 */
	public function loadCssPath($path){
		$this->view->headLink()->appendStylesheet($path);
	}
	
	/**
	 * 
	 * Load icon cho the tab khi duyet web
	 * @param unknown_type $iconName: ten file icon vd : ipco.png
	 */
	public function loadIconInTab($iconName,$nameFolderRoot){
		$this->view->headLink()->headLink(array('rel' => 'icon',  
                                      'href' => $nameFolderRoot.'/public/'.$iconName,  
                                      'type' =>'image/x-icon',  
               ));  
	}
	
	public function loadJs($pathAndNameJs){
		$this->view->headScript()->appendFile($pathAndNameJs);
	}
	public function jShowMessage($message){
		echo '<script type="text/javascript">
				alert("'.$message.'");
			</script>';
	}
	
	/**
	 * 
	 * Doc du lieu tu file CSV (dang cua excel) cho vao mang
	 * @param unknown_type $filename : ten file can xu ly
	 * @param unknown_type $numCols : tong so cac cot trong bang CSDL MySql
	 * @return : Array
	 */
	public function processFileCSV ($filename,$numCols) {
	    $rtn = array();
	
	    if (($handle = fopen($filename, "r")) !== FALSE) {
	        
	        while (($data = fgetcsv($handle, 10000, ",")) !== FALSE) {            
	            $item = array();
	            for($i=0; $i<$numCols; $i++){
	                $item[] = $data[$i];                
	            }
	            $rtn[] = $item;            
	        }
	    }
	    return $rtn;
 	}
 	
 	/**
 	 * 
 	 * Lay du lieu trong Array doc duoc tu file bo vao cau lenh Sql
 	 * @param unknown_type $tableName: Ten cua bang
 	 * @param unknown_type $numCols: so cot trong bang
 	 * @param unknown_type $dataArray : Du lieu doc duoc tu file
 	 */
 	public function importFileCSV($tableName,$numCols, $dataArray){
	 	$arrSql =array(); 
	    //lay ten cac cot
	    $sql = "INSERT INTO {$tableName} (";
	    for($i=0; $i< $numCols ; $i++){
	        $sql .= $dataArray[0][$i] .",";
	    }    
	    
	    //
	    //cat bo bot dau phay o cuoi cung
	    $sql = substr($sql,0, -1);
	    $sql .= ") VALUES (";
	    
	    for($i=1; $i<count($dataArray); $i++){
	        //gan du lieu cho phan values
	        $temp =$sql;
	        for($j=0; $j<$numCols; $j++){
	            $temp .= "'{$dataArray[$i][$j]}' ,";    
	        }
	        
	        $temp = substr($temp,0,-1) .")";
	        
	        $arrSql[] = $temp; 
	    }
 	}
 	////////////////////////////////////////////////////////////////////////
 	////	EXAME CONTROLLER IN FRONT
 	///////////////////////////////////////////////////////////////////////
 	
 	/**
 	 * 
 	 * Luu vao file thong tin ngay lÃ m va tÃ¬nh trang lam bai exame
 	 * @param unknown_type $user
 	 * @param unknown_type $date
 	 */
	public function setDoExame($idExame,$username,$date){
        	$file=fopen(WEB_ROOT."/docs/files/".$username.".data","a");
        	$str = "1|".$idExame."|".$date."\r\n";
        	
        	fwrite($file,$str);
        	fclose($file);
 	}
 	
 	public function isMadeExame($username, $idKiemTra,$date){
 		if(file_exists(WEB_ROOT."/docs/files/".$username.".data")){
	 		$file = fopen(WEB_ROOT."/docs/files/".$username.".data","r");

	 		
	 		$dateArr2 = explode("/", $date);
	 		$dateDeadline  = @mktime(0,0,0, $dateArr2[1],$dateArr2[0],$dateArr2[2]);
	 		$flag = false;
	 		
	 		while (!feof($file)){
	 			$line= fgets($file);
	 			
	 			if(!empty($line) || $line!=null || strlen($line)>0){
	 				$arr = explode("|", $line);

		 			$dateArr1 = explode("/", $arr[2]);
		 			$dateNow = @mktime(0,0,0,$dateArr1[1],$dateArr1[0],$dateArr1[2]);
		 			
		 			if(($dateNow <= $dateDeadline) && ($arr[1] == $idKiemTra) && $arr[0]==1
		 				|| ($dateNow > $dateDeadline && ($arr[1] == $idKiemTra) && $arr[0] == 1) ) {
	 						return true;
		 			}
	 			}
	 		}
	        
	        fclose($file);

        	return false;
 		}else 
 			return FALSE;
 	}
 	
 	/**
 	 * 
 	 */
 	public function jDialogMessage($title='Thông báo',$message){
 		echo'
 			<div id="jdialog" title="'.$title.'">
				<p style="font-size: 1.4em;">'.$message.'.</font></p>
			</div>
	 		<script>
				$(function() {
					$( "#jdialog" ).dialog();
				});
			</script>';
 	}
 	
 	public function jCommand($stringScript){
 		echo'<script type="text/javascript">'.
			$stringScript
 			.'</script>';
 	}
}